package com.tanxi521.springboot.aop;

import com.tanxi521.springboot.mapper.PlayerAccountMapper;
import com.tanxi521.springboot.pojo.APIResult;
import com.tanxi521.springboot.pojo.OaUser;
import com.tanxi521.springboot.pojo.PlayerAccount;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: i-xiaotian
 * @Date: 2022/3/25 15:32
 * @EMAIL: 1254806967@qq.com
 */
@Component
@Aspect
@Order(1)
public class UserPermissionAop {

    @Autowired
    RedisTemplate redisTemplate;

    @Pointcut("@annotation(com.tanxi521.springboot.pojo.RestPoint)")
    public void restAop() {
    }

    @Pointcut("@annotation(com.tanxi521.springboot.pojo.OpNeed)")
    public void opPermission() {
    }

    @Pointcut("@annotation(com.tanxi521.springboot.pojo.RegHeader)")
    public void onReg() {
    }

    @Value("${reg.header}")
    private String regHeader;

    @Value("${reg.header.value}")
    public String headerValue;

    @Around("opPermission()")
    public Object needOp(ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("X-Token");
        if (token != null) {
            OaUser oaUser = (OaUser)redisTemplate.opsForValue().get(token);
            Integer userRole = oaUser.getUserRole();
            if (userRole != 1) {
                return new APIResult(APIResult.FAIL, "你没有权限");
            }
            try {
                return joinPoint.proceed();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                return new APIResult(APIResult.FAIL, "系统错误，请重试");
            }
        } else {
            return new APIResult(APIResult.UN_LOGIN, "请先登录");
        }
    }

    @Around("restAop()")
    public Object integral(ProceedingJoinPoint joinPoint) {
        Object result = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request != null) {
            String token = request.getHeader("X-Token");
            if (token == null) {
                result = new APIResult(APIResult.UN_LOGIN, "非法token");
                return result;
            }
            OaUser oa_user = (OaUser)redisTemplate.opsForValue().get(token);
            if (oa_user == null) {
                result = new APIResult(APIResult.TOKEN_EXPIRED, "请先登陆");
            } else {
                try {
                    result = joinPoint.proceed();
                } catch (Throwable throwable) {
                    throwable.printStackTrace();
                    result = new APIResult(APIResult.FAIL, "系统错误，请稍后再试/.");
                }
            }
            return result;
        }
        return null;
    }

    @Around("onReg()")
    public Object regAop(ProceedingJoinPoint joinPoint) {
        Object result = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String regToken = request.getHeader(regHeader);
        if (StringUtils.isAllEmpty(regToken)) {
            return new APIResult(APIResult.FAIL, "错误请求");
        }
        if (!regToken.equals(headerValue)) {
            return new APIResult(APIResult.FAIL, "错误请求");
        }
        try {
            result = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return new APIResult(APIResult.FAIL, "错误请求XX1");
        }
        return result;
    }


}
